/**
 * 系统内修改密码与第三方修改密码的公用部分
 * 请不要在里面写http请求
 * created: yj
 * date: 20200306
 */

import debounce from 'lodash/debounce'
export default {
  data () {
    return {
      visible: false,
      dataForm: {
        username: '',
        password: '',
        newPassword: '',
        comfirmPassword: ''
      },
      // 当前密码长度
      password_length: 0,
      // 显示或隐藏密码长度状态栏
      typed: false,
      // 包含小写
      contains_lovercase: false,
      // 包含数字
      contains_number: false,
      // 包含大写
      contains_uppercase: false,
      // 包含大写特殊
      contains_special: false,
      // 密码长度验证结果
      valid_password_length: false,
      // 密码验证结果
      valid_password: false,
      // 当前密码等级
      pwdLevel: 0,
      // 设置密码等级
      pwdSecurityLevel: 4,
      // 显示取消按钮
      showCancelButton: true
    }
  },
  computed: {
    dataRule () {
      var validateComfirmPassword = (rule, value, callback) => {
        if (this.dataForm.newPassword !== value) {
          return callback(new Error(this.$t('updatePassword.validate.comfirmPassword')))
        }
        callback()
      }
      var validateNewPassword = (rule, value, callback) => {
        if (!this.valid_password) {
          return callback(new Error(this.$t('updatePassword.validate.pwdLevelMessage' + this.pwdSecurityLevel)))
        }
        callback()
      }
      return {
        username: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        password: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        newPassword: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' },
          { validator: validateNewPassword, trigger: 'blur' }
        ],
        comfirmPassword: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' },
          { validator: validateComfirmPassword, trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    init () {
      this.visible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].resetFields()
        this.p_len()
        this.getInfo()
      })
    },
    // 验证密码
    p_len: function () {
      // 获取密码长度
      this.password_length = this.dataForm.newPassword.length
      // 判断密码长度是否符合最小长度
      if (this.password_length >= (this.pwdSecurityLevel <= 1 ? 6 : 8)) {
        this.valid_password_length = true
      } else {
        this.valid_password_length = false
      }
      // 输入密码后显示密码等级div
      this.typed = this.password_length > 0
      // 验证密码安全程度
      this.contains_lovercase = /[a-z]/.test(this.dataForm.newPassword)
      this.contains_number = /\d/.test(this.dataForm.newPassword)
      this.contains_uppercase = /[A-Z]/.test(this.dataForm.newPassword)
      this.contains_special = /[~!@#$%^&*()_+{}:"|<>?`\-=[\];'\\,./]/.test(this.dataForm.newPassword)
      this.pwdLevel = (this.password_length >= 8 ? 1 : 0) + (this.contains_lovercase ? 1 : 0) + (this.contains_number ? 1 : 0) + (this.contains_uppercase ? 1 : 0) + (this.contains_special ? 1 : 0)
      if (!this.valid_password_length) {
        // 如果不符合最低密码长度设为简单
        this.pwdLevel = 1
      } else if (this.pwdLevel === 3 || this.pwdLevel === 4) {
        this.pwdLevel = 3
      } else if (this.pwdLevel === 5) {
        this.pwdLevel = 4
      }
      // 判断是否满足
      if (this.valid_password_length && this.pwdLevel >= this.pwdSecurityLevel) {
        this.valid_password = true
      } else {
        this.valid_password = false
      }
    },
    // 获取信息
    getInfo () { },
    // 表单提交
    dataFormSubmitHandle: debounce(function () {
    }, 1000, { 'leading': true, 'trailing': false })
  }
}
